<?php  
	include($GLOBALS['BIB'].'Pr_db/pr_historics.mex');
	include($GLOBALS['BIB'].'Objects/val.form.mex');
	// Set Vars
	$ApllyAddress = 0;
	$ApllyContact = 0;
	$RemoveLock = '';

	$_POST['CL_CTT_CliCttEmail'] = strtolower($_POST['CL_CTT_CliCttEmail']);
	$_POST['CL_CTT_CliDomicilio'] = req_form($_POST['CL_CTT_CliDomicilio']);
	$_POST['CL_CTT_CliPisoPuerta'] = req_form($_POST['CL_CTT_CliPisoPuerta']);
	$_POST['CL_CTT_CliPortal'] = req_form($_POST['CL_CTT_CliPortal']);
	$_POST['CL_CTT_CliLocation'] = req_form($_POST['CL_CTT_CliLocation']);
	$_POST['CliActividad'] = req_form($_POST['CliActividad']);
	if(isset($_POST['CLCttEmailExists'])){$_POST['CL_CTT_CliCttEmail']=0;
	}elseif(frm_verifEmail($_POST['CL_CTT_CliCttEmail']) === 0 && $_POST['CL_CTT_CliCttEmail'] != ''){
		$error = 'Email invalido.';
	}

	if(!isset($error)){
	$Get_OldRecord = $GLOBALS['MYT']->db_array("SELECT 
		CliDomicilio,
		CliPisoPuerta,
		CliPortal,
		CliCP,
		ClPais,
		CliProvincia,
		CliCiudad,
		CliLocation,
		CliTelefono,
		CliTelefono2,
		CliBloqueado,
		CLCttEmail,
		CliActividad,
		CliMailing,
		CliExtID,
		bac_ID	
	FROM clientes 
		LEFT OUTER JOIN clientes_ext ON CliNumero = CliExtNumero
		LEFT OUTER JOIN bonusaccounts ON CliNumero = bac_Client
		WHERE CliNumero = {$_POST['CliNumero']}");
	if ($Get_OldRecord[1]['CliBloqueado'] == 2) { $RemoveLock = ', CliBloqueado = 0'; }
	if (!isset($Get_OldRecord[1]['CliExtID'])) { 
		$GLOBALS['MYT']->db_query("INSERT INTO clientes_ext (CliExtNumero) VALUES ({$_POST['CliNumero']})");
	}

	if ($_POST['CL_ELEMTYPE2']==1) {


		if ($Get_OldRecord[1]['CliDomicilio'] != $_POST['CL_CTT_CliDomicilio'] || $Get_OldRecord[1]['CliPisoPuerta'] != $_POST['CL_CTT_CliPisoPuerta'] || $Get_OldRecord[1]['CliPortal'] != $_POST['CL_CTT_CliPortal'] || $Get_OldRecord[1]['CliCP'] != $_POST['CL_CTT_CliCP'] || $Get_OldRecord[1]['ClPais'] != $_POST['CL_CTT_CliPais'] || $Get_OldRecord[1]['CliProvincia'] != $_POST['CL_CTT_CliProvincia'] || $Get_OldRecord[1]['CliCiudad'] != $_POST['CL_CTT_CliCiudad'] || $Get_OldRecord[1]['CliLocation'] != $_POST['CL_CTT_CliLocation']) {
			
				$qr_up2 = "UPDATE clientes 
				SET 
					CliDomicilio = '{$_POST['CL_CTT_CliDomicilio']}',
					CliPisoPuerta = '{$_POST['CL_CTT_CliPisoPuerta']}',
					CliPortal = '{$_POST['CL_CTT_CliPortal']}',
					CliCP = '{$_POST['CL_CTT_CliCP']}',
					ClPais = {$_POST['CL_CTT_CliPais']},
					CliProvincia  = '{$_POST['CL_CTT_CliProvincia']}',
					CliCiudad = '{$_POST['CL_CTT_CliCiudad']}',
					CliLocation = '{$_POST['CL_CTT_CliLocation']}'
				WHERE CliNumero = {$_POST['CliNumero']}";
		}
				$GLOBALS['MYT']->db_query("UPDATE clientes_ext SET 
				CliDataVal2 = '{$sysTime}'
				WHERE CliExtNumero = {$_POST['CliNumero']}");
	}
	if ($_POST['CL_ELEMTYPE1']==1) {
		if ($Get_OldRecord[1]['CliMailing'] != $_POST['CliContacto'] || $Get_OldRecord[1]['CliTelefono'] != $_POST['CL_CTT_CliTelefono'] || $Get_OldRecord[1]['CliTelefono2'] != $_POST['CL_CTT_CliTelefono2'] || $Get_OldRecord[1]['CLCttEmail'] !== $_POST['CL_CTT_CliCttEmail']) {
		
			$qr_up1 = "UPDATE clientes 
			SET 
				CliTelefono = '{$_POST['CL_CTT_CliTelefono']}',
				CliTelefono2 = '{$_POST['CL_CTT_CliTelefono2']}',
				ClCttEmail = '{$_POST['CL_CTT_CliCttEmail']}',
				CliMailing = {$_POST['CliContacto']}
				{$RemoveLock}
			WHERE CliNumero = {$_POST['CliNumero']}";
		} 	
				$GLOBALS['MYT']->db_query("UPDATE clientes_ext SET 
				CliDataVal1 = '{$sysTime}'
				WHERE CliExtNumero = {$_POST['CliNumero']}");
	}
	if ($_POST['CL_ELEMTYPE3']==1) {
		if ($Get_OldRecord[1]['CliActividad'] != $_POST['CliActividad']) {
		
			$qr_up3 = "UPDATE clientes 
			SET 
				CliActividad = '{$_POST['CliActividad']}'
			WHERE CliNumero = {$_POST['CliNumero']}";
		} 	
	} 
		if (isset($qr_up1) || isset($qr_up2) || isset($qr_up3)){
		hs_client($_POST['CliNumero'],$sysTime);
		
			if (isset($qr_up1)){
		$GLOBALS['MYT']->db_query($qr_up1);
		$GLOBALS['MYT']->db_query("INSERT INTO hs_dataval (hlv_Numero, hlv_Field, hlv_User)
		VALUES ({$_POST['CliNumero']},1,{$_SESSION[$SESSID]})");
			}
		
			if (isset($qr_up2)){
		$GLOBALS['MYT']->db_query($qr_up2);
		$GLOBALS['MYT']->db_query("INSERT INTO hs_dataval (hlv_Numero, hlv_Field, hlv_User)
		VALUES  ({$_POST['CliNumero']},2,{$_SESSION[$SESSID]})");
			}
			if (isset($qr_up3)){
		$GLOBALS['MYT']->db_query($qr_up3);
		$GLOBALS['MYT']->db_query("INSERT INTO hs_dataval (hlv_Numero, hlv_Field, hlv_User)
		VALUES  ({$_POST['CliNumero']},3,{$_SESSION[$SESSID]})");
			}
		}
	if (!isset($SYSMOD)) { $SYSMOD = 'XFK0WK2A';}
	echo '<div class="Confirm">Concluido</div>
	<script charset="utf-8" type="text/javascript">Ext.getCmp(\'winclupdate\').close();
	replaceUrl(\'contents\',\'_response/call.php?get=Operative/client&CID='.$_POST['CliNumero'].'&MOD='.$SYSMOD.'\');</script>';
	} else {
	echo '<div class="Error">'.$error.'</div>
	<script charset="utf-8" type="text/javascript">
	document.getElementById(\'CL_CTT_SAVE\').disabled=false;</script>';
	}